package com.hm.PushCaseService.control;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.PushCaseService.framework.service.base.IBaseService;
import com.hm.PushCaseService.framework.service.token.ITokenService;
import com.hm.PushCaseService.pojo.CaseBean;
import com.hm.PushCaseService.service.caseinfo.ICase;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping(value = "/main", produces = {"application/json;charset=UTF-8"})
public class PCS_000_000_004<T> {

    private static final Logger logger = LoggerFactory.getLogger(PCS_000_000_004.class);

    private final IBaseService<List<CaseBean>> iBaseService;

    private final ITokenService iTokenService;

    private final ICase iCase;

    //获取最新案件
    public PCS_000_000_004(ITokenService iTokenService, IBaseService<List<CaseBean>> iBaseService, ICase iCase) {
        this.iBaseService = iBaseService;
        this.iTokenService = iTokenService;
        this.iCase = iCase;
    }

    //获取最新案件
    @PostMapping(value = "/000_000_004")
    public String doPost(HttpServletRequest request) {
        //基本处理（校验session合法性）
        HashMap<String, String> resMap = iBaseService.handleRequest(request);
        String state = resMap.get("state");
        String msg = resMap.get("msg");
        String body = resMap.get("body");
        String secretKey = resMap.get("secretKey");
        if (!state.equals("000")) {
            return iBaseService.generateFail(msg);
        }
        //校验token
        JSONObject jsonObject = (JSONObject) JSON.parse(body);
        String token = jsonObject.getString("token");
        HashMap<String, String> tokenResMap = iTokenService.verifier(token);
        String tokenState = tokenResMap.get("state");
        String tokenMsg = tokenResMap.get("msg");
        if (!tokenState.equals("000")) {
            return iBaseService.generateFail(tokenMsg);
        }
        List<CaseBean> caseBeans = iCase.queryCurrentDayCase();
        return iBaseService.generateSucceed(secretKey, caseBeans);
    }


}
